# Maintainer: Saul Ibarra Corretge <saghul@gmail.com>

_realname=pyqt5
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-common" "${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=5.10.1
#_pre_ver=5.8.dev1702091323
pkgrel=1
pkgdesc="Qt5 bindings for Python (mingw-w64)"
arch=('any')
license=('GPL')
url="http://www.riverbankcomputing.co.uk/software/pyqt"
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-sip"
             "${MINGW_PACKAGE_PREFIX}-python3-sip"
             "${MINGW_PACKAGE_PREFIX}-qt5"
             "${MINGW_PACKAGE_PREFIX}-qtwebkit"
             "${MINGW_PACKAGE_PREFIX}-make")
options=('strip' 'staticlibs')
source=("https://downloads.sourceforge.net/project/pyqt/PyQt5/PyQt-${pkgver}/PyQt5_gpl-${pkgver}.tar.gz"
        #https://www.riverbankcomputing.com/static/Downloads/PyQt5/PyQt5_gpl-${_pre_ver}.tar.gz
        001-mingw-python.patch)
sha256sums=('9932e971e825ece4ea08f84ad95017837fa8f3f29c6b0496985fa1093661e9ef'
            '7d950551cbe39d0f9d191def17bd05c430525a5a80e9cc491d1f22c2d786fb47')

prepare() {
 cd "${srcdir}"/PyQt5_gpl-${pkgver}

  #Apply patches
  patch -p1 -i "${srcdir}"/001-mingw-python.patch
}

build() {
  for builddir in python{2,3}-${MINGW_CHOST}; do
    [[ -d $builddir ]] && rm -rf ${builddir}
    cp -r PyQt5_gpl-${pkgver} ${builddir}
    pushd ${builddir} > /dev/null
    [[ "${builddir%-${MINGW_CHOST}}" == "python2" ]] && {
      local _sipdir=Py2-Qt5
    } || {
      local _sipdir=PyQt5
    }
    ${MINGW_PREFIX}/bin/${builddir%-${MINGW_CHOST}} configure.py \
      --confirm-license \
      --qsci-api \
      --sipdir ${MINGW_PREFIX}/share/sip/${_sipdir} \
      -q ${MINGW_PREFIX}/bin/qmake.exe \
      --verbose
    make V=1
    popd > /dev/null
  done
}

package_pyqt5-common(){
  pkgdesc="Common PyQt files shared between python3-pyqt5 and python2-pyqt5"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5" "${MINGW_PACKAGE_PREFIX}-qtwebkit")

  cd python2-${MINGW_CHOST}
  #make -C pyrcc DESTDIR="${pkgdir}" INSTALL_ROOT="${pkgdir}" install
  #make -C pylupdate DESTDIR="${pkgdir}" INSTALL_ROOT="${pkgdir}" install

  install -Dm644 PyQt5.api "${pkgdir}${MINGW_PREFIX}"/share/qt5/qsci/api/python/PyQt5.api
}

package_python3-pyqt5(){
  pkgdesc="A set of Python 3.x bindings for the Qt5 toolkit"
  depends=("${MINGW_PACKAGE_PREFIX}-python3-sip" "${MINGW_PACKAGE_PREFIX}-pyqt5-common" "${MINGW_PACKAGE_PREFIX}-python3")
  #optdepends=('python-opengl: enable OpenGL 3D graphics in PyQt applications'
  #            'python-dbus: for python-dbus mainloop support')

  cd python3-${MINGW_CHOST}
  # INSTALL_ROOT is needed for the QtDesigner module, the other Makefiles use DESTDIR
  make DESTDIR="${pkgdir}" INSTALL_ROOT="${pkgdir}" install

  local _py3basever=$(${MINGW_PREFIX}/bin/python3 -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  # Provided by pyqt-common
  #rm "${pkgdir}${MINGW_PREFIX}"/bin/{pylupdate5,pyrcc5}.exe
  rm "${pkgdir}${MINGW_PREFIX}"/share/qt5/qsci/api/python/PyQt5.api
  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib/python${_py3basever}/site-packages/PyQt5/uic/port_v2
}

package_python2-pyqt5(){
  pkgdesc="A set of Python 2.x bindings for the Qt5 toolkit"
  depends=("${MINGW_PACKAGE_PREFIX}-python2-sip" "${MINGW_PACKAGE_PREFIX}-pyqt5-common" "${MINGW_PACKAGE_PREFIX}-python2")
  #optdepends=('python2-opengl: enable OpenGL 3D graphics in PyQt applications'
  #            'python-dbus: for python-dbus mainloop support')

  cd python2-${MINGW_CHOST}
  # INSTALL_ROOT is needed for the QtDesigner module, the other Makefiles use DESTDIR
  make DESTDIR="${pkgdir}" INSTALL_ROOT="${pkgdir}" install

  local _py2basever=$(${MINGW_PREFIX}/bin/python2 -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  # Fix conflicts with python-pyqt5
  mv "${pkgdir}${MINGW_PREFIX}"/bin/{,python2-}pyuic5.bat
  mv "${pkgdir}${MINGW_PREFIX}"/bin/{,python2-}pylupdate5.bat
  mv "${pkgdir}${MINGW_PREFIX}"/bin/{,python2-}pyrcc5.bat

  #rm "${pkgdir}${MINGW_PREFIX}"/bin/{pylupdate5,pyrcc5}.exe
  rm "${pkgdir}${MINGW_PREFIX}"/share/qt5/plugins/designer/pyqt5.dll
  rm "${pkgdir}${MINGW_PREFIX}"/share/qt5/plugins/PyQt5/pyqt5qmlplugin.dll
  rm "${pkgdir}${MINGW_PREFIX}"/share/qt5/qsci/api/python/PyQt5.api
  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib/python${_py2basever}/site-packages/PyQt5/uic/port_v3
}

# Wrappers
package_mingw-w64-i686-pyqt5-common() {
  package_pyqt5-common
}

package_mingw-w64-i686-python2-pyqt5() {
  package_python2-pyqt5
}

package_mingw-w64-i686-python3-pyqt5() {
  package_python3-pyqt5
}

package_mingw-w64-x86_64-pyqt5-common() {
  package_pyqt5-common
}

package_mingw-w64-x86_64-python2-pyqt5() {
  package_python2-pyqt5
}

package_mingw-w64-x86_64-python3-pyqt5() {
  package_python3-pyqt5
}
